home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1994 August: Tool Chest / Dev.CD Aug 94.toast / New System Software Extensions / OpenDoc A6 / OpenDoc Parts Framework / OPF / OS / FWGraphx / Sources / FWStyle.cpp < prev    next >
Encoding:
Text File  |  1994-04-21  |  11.8 KB  |  443 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                FWStyle.cpp
  4. //    Release Version:    $ 1.0d1 $
  5. //
  6. //    Creation Date:        3/28/94
  7. //
  8. //    Copyright:    © 1993, 1994 by Apple Computer, Inc., all rights reserved.
  9. //
  10. //========================================================================================
  11.  
  12. #ifndef FWSTYLE_H
  13. #include "FWStyle.h"
  14. #endif
  15.  
  16. #ifndef FWGC_H
  17. #include "FWGC.h"
  18. #endif
  19.  
  20. #ifndef FWPOINT_H
  21. #include "FWPoint.h"
  22. #endif
  23.  
  24. // ----- Macintosh Includes -----
  25.  
  26. #if defined(FW_BUILD_MAC) && !defined(__TEXTUTILS__)
  27. #include <TextUtils.h>
  28. #endif
  29.  
  30. #if defined(FW_BUILD_MAC) && !defined(__FONTS__)
  31. #include <Fonts.h>
  32. #endif
  33.  
  34. #if defined(FW_BUILD_MAC) && !defined(mathRoutinesIncludes)
  35. #include <math routines.h>
  36. #endif
  37.  
  38. //==============================================================================
  39. //    •• RunTime Info
  40. //==============================================================================
  41.  
  42. #ifdef FW_BUILD_MAC
  43. #pragma segment fwgraphx
  44. #endif
  45.  
  46. //==============================================================================
  47. //    •• class FW_CStyle
  48. //==============================================================================
  49.  
  50. //------------------------------------------------------------------------------
  51. // FW_CStyle::FW_CStyle
  52. //------------------------------------------------------------------------------
  53.  
  54. FW_CStyle::FW_CStyle() :
  55.     FW_CGraphicObjectPtr()
  56. {
  57. }
  58.  
  59. //------------------------------------------------------------------------------
  60. // FW_CStyle::FW_CStyle
  61. //------------------------------------------------------------------------------
  62.  
  63. FW_CStyle::FW_CStyle(XMPCoordinate penSize, const FW_CPattern& pattern) :
  64.     FW_CGraphicObjectPtr()
  65. {
  66.     SetRep(new FW_CStyleRep(penSize, pattern));
  67. }
  68.  
  69. //------------------------------------------------------------------------------
  70. // FW_CStyle::FW_CStyle
  71. //------------------------------------------------------------------------------
  72.  
  73. FW_CStyle::FW_CStyle(const FW_CDynamicString& fontName, FW_FontStyle fontStyle, XMPCoordinate fontSize) :
  74.     FW_CGraphicObjectPtr()
  75. {
  76.     SetRep(new FW_CStyleRep(fontName, fontStyle, fontSize));
  77. }
  78.  
  79. //------------------------------------------------------------------------------
  80. // FW_CStyle::FW_CStyle
  81. //------------------------------------------------------------------------------
  82.  
  83. FW_CStyle::FW_CStyle(XMPCoordinate penSize,
  84.                     const FW_CPattern& pattern,
  85.                     const FW_CDynamicString& fontName,
  86.                     FW_FontStyle fontStyle,
  87.                     XMPCoordinate fontSize) :
  88.     FW_CGraphicObjectPtr()
  89. {
  90.     SetRep(new FW_CStyleRep(penSize, pattern, fontName, fontStyle, fontSize));
  91. }
  92.  
  93. //------------------------------------------------------------------------------
  94. // FW_CStyle::FW_CStyle
  95. //------------------------------------------------------------------------------
  96.  
  97. FW_CStyle::FW_CStyle(const FW_CStyle& other) :
  98.     FW_CGraphicObjectPtr(other)
  99. {
  100. }
  101.  
  102. //------------------------------------------------------------------------------
  103. // FW_CStyle::FW_CStyle
  104. //------------------------------------------------------------------------------
  105.  
  106. FW_CStyle::FW_CStyle(FW_CStyleRep* rep) :
  107.     FW_CGraphicObjectPtr(rep)
  108. {
  109. }
  110.  
  111. //------------------------------------------------------------------------------
  112. // FW_CStyle::operator=
  113. //------------------------------------------------------------------------------
  114.  
  115. FW_CStyle& FW_CStyle::operator=(const FW_CStyle& other)
  116. {
  117.     SetRep(other.GetRep());
  118.     return *this;
  119. }
  120.  
  121. //==============================================================================
  122. //    •• class FW_CStyleRep
  123. //==============================================================================
  124.  
  125. //------------------------------------------------------------------------------
  126. //    • FW_CStyleRep::FW_CStyleRep
  127. //------------------------------------------------------------------------------
  128.  
  129. FW_CStyleRep::FW_CStyleRep() :
  130.     fPenSize(ff(1)),
  131.     fPattern(FW_kBlackPat),
  132.     fFontName(""),
  133.     fFontStyle(FW_kPlain),
  134.     fFontSize(ff(12)),
  135. #ifdef FW_BUILD_MAC
  136.     fMacTextFont(applFont),
  137.     fMacTextFace(normal),
  138.     fMacCacheValid(TRUE)
  139. #endif
  140. {
  141. }
  142.  
  143. //------------------------------------------------------------------------------
  144. //    • FW_CStyleRep::FW_CStyleRep
  145. //------------------------------------------------------------------------------
  146.  
  147. FW_CStyleRep::FW_CStyleRep(XMPCoordinate penSize, const FW_CPattern& pattern) :
  148.     fPenSize(penSize),
  149.     fPattern(pattern),
  150.     fFontName(""),
  151.     fFontStyle(FW_kPlain),
  152.     fFontSize(ff(12)),
  153. #ifdef FW_BUILD_MAC
  154.     fMacTextFont(applFont),
  155.     fMacTextFace(normal),
  156.     fMacCacheValid(TRUE)
  157. #endif
  158. {
  159. }
  160.  
  161. //------------------------------------------------------------------------------
  162. //    • FW_CStyleRep::FW_CStyleRep
  163. //------------------------------------------------------------------------------
  164.  
  165. FW_CStyleRep::FW_CStyleRep(const FW_CDynamicString& fontName, FW_FontStyle fontStyle, XMPCoordinate fontSize) :
  166.     fPenSize(ff(1)),
  167.     fPattern(FW_kBlackPat),
  168.     fFontName(fontName),
  169.     fFontStyle(FW_kPlain),
  170.     fFontSize(ff(12)),
  171. #ifdef FW_BUILD_MAC
  172.     fMacTextFont(applFont),
  173.     fMacTextFace(normal),
  174.     fMacCacheValid(FALSE)
  175. #endif
  176. {
  177. }
  178.  
  179. //------------------------------------------------------------------------------
  180. //    • FW_CStyleRep::FW_CStyleRep
  181. //------------------------------------------------------------------------------
  182.  
  183. FW_CStyleRep::FW_CStyleRep(XMPCoordinate penSize,
  184.                             const FW_CPattern& pattern,
  185.                             const FW_CDynamicString& fontName,
  186.                             FW_FontStyle fontStyle,
  187.                             XMPCoordinate fontSize) :
  188.     fPenSize(penSize),
  189.     fPattern(pattern),
  190.     fFontName(fontName),
  191.     fFontStyle(fontStyle),
  192.     fFontSize(fontSize),
  193. #ifdef FW_BUILD_MAC
  194.     fMacTextFont(applFont),
  195.     fMacTextFace(normal),
  196.     fMacCacheValid(FALSE)
  197. #endif
  198. {
  199. }
  200.  
  201. //------------------------------------------------------------------------------
  202. //    • FW_CStyleRep::~FW_CStyleRep
  203. //------------------------------------------------------------------------------
  204.  
  205. FW_CStyleRep::~FW_CStyleRep()
  206. {
  207. }
  208.  
  209. //------------------------------------------------------------------------------
  210. //    • FW_CStyleRep::AsPlatformSize
  211. //------------------------------------------------------------------------------
  212.  
  213. FW_SPlatformPoint FW_CStyleRep::AsPlatformSize(FW_CGraphicContext* graphicContext, XMPCoordinate aSize)
  214. {
  215.     FW_CPoint s(aSize, aSize);
  216.     FW_SPlatformPoint qdSize = graphicContext->AsPlatformPoint(s);
  217.     return qdSize;
  218. }
  219.  
  220. //------------------------------------------------------------------------------
  221. //    • FW_CStyleRep::SelectInGC
  222. //------------------------------------------------------------------------------
  223.  
  224. void FW_CStyleRep::SelectInGC(FW_CGraphicContext* gc, FW_ShapeCategory shapeCategory)
  225. {
  226. #ifdef FW_BUILD_MAC
  227.     if (shapeCategory == FW_kGeometricShape)
  228.     {
  229.         FW_SPlatformPoint penSize = AsPlatformSize(gc, fPenSize);
  230.         ::PenSize(penSize.h, penSize.v);
  231.         ::PenPat(fPattern);
  232.     }
  233.     else if (shapeCategory == FW_kTypographicShape)
  234.     {
  235.         CheckMacCache();
  236.         ::TextFont(fMacTextFont);
  237.         ::TextFace(fMacTextFace);
  238.         ::TextSize(FixedToInt(fFontSize));
  239.     }
  240. #endif
  241. }
  242.  
  243. //----------------------------------------------------------------------------------------
  244. //    • FW_CStyleRep::Copy
  245. //----------------------------------------------------------------------------------------
  246.  
  247. FW_CStyle FW_CStyleRep::Copy() const
  248. {
  249.     FW_CStyle style(fPenSize, fPattern, fFontName, fFontStyle, fFontSize);
  250.     return style;
  251. }
  252.  
  253. //------------------------------------------------------------------------------
  254. //    • FW_CStyleRep::SetPenSize
  255. //------------------------------------------------------------------------------
  256.  
  257. void FW_CStyleRep::SetPenSize(XMPCoordinate penSize)
  258. {
  259.     fPenSize = penSize;
  260.     Changed();
  261. }
  262.  
  263. //------------------------------------------------------------------------------
  264. //    • FW_CStyleRep::SetPattern
  265. //------------------------------------------------------------------------------
  266.  
  267. void FW_CStyleRep::SetPattern(const FW_CPattern& pattern)
  268. {
  269.     fPattern = pattern;
  270.     Changed();
  271. }
  272.  
  273. //------------------------------------------------------------------------------
  274. //    • FW_CStyleRep::SetFontName
  275. //------------------------------------------------------------------------------
  276.  
  277. void FW_CStyleRep::SetFontName(const FW_CDynamicString& fontName)
  278. {
  279.     fFontName = fontName;
  280.  
  281. #ifdef FW_BUILD_MAC
  282.     fMacCacheValid = FALSE;
  283. #endif
  284.  
  285.     Changed();
  286. }
  287.  
  288. //------------------------------------------------------------------------------
  289. //    • FW_CStyleRep::SetFontStyle
  290. //------------------------------------------------------------------------------
  291.  
  292. void FW_CStyleRep::SetFontStyle(FW_FontStyle fontStyle)
  293. {
  294.     fFontStyle = fontStyle;
  295.     
  296. #ifdef FW_BUILD_MAC
  297.     fMacCacheValid = FALSE;
  298. #endif
  299.  
  300.     Changed();
  301. }
  302.  
  303. //------------------------------------------------------------------------------
  304. //    • FW_CStyleRep::SetFontSize
  305. //------------------------------------------------------------------------------
  306.  
  307. void FW_CStyleRep::SetFontSize(XMPCoordinate fontSize)
  308. {
  309.     fFontSize = fontSize;
  310.     Changed();
  311. }
  312.  
  313. #ifdef FW_BUILD_MAC
  314. //------------------------------------------------------------------------------
  315. //    • FW_CStyleRep::GetMacFontInfo
  316. //------------------------------------------------------------------------------
  317.  
  318. void FW_CStyleRep::GetMacFontInfo(short& textFont, 
  319.                                     Style& textFace, 
  320.                                     XMPCoordinate& textSize) const
  321. {
  322.     textFont = fMacTextFont;
  323.     textFace = fMacTextFace;
  324.     textSize = fFontSize;
  325. }
  326. #endif
  327.  
  328. //----------------------------------------------------------------------------------------
  329. //    • FW_CStyleRep::Flatten
  330. //----------------------------------------------------------------------------------------
  331.  
  332. void FW_CStyleRep::Flatten(FW_CWritableStream& stream)
  333. {
  334.     stream.Write(&fPenSize, sizeof(fPenSize));
  335.     fPattern.Flatten(stream);
  336.     FW_ByteCount count = fFontName.GetByteLength();
  337.     stream << count;
  338.     if (count != 0)
  339.         stream.Write(fFontName, count);
  340.     stream.Write((char*)&fFontStyle, sizeof(fFontStyle));
  341.     stream << fFontSize;
  342. }
  343.  
  344. //----------------------------------------------------------------------------------------
  345. //    • FW_CStyleRep::Unflatten
  346. //----------------------------------------------------------------------------------------
  347.  
  348. void FW_CStyleRep::Unflatten(FW_CReadableStream& stream)
  349. {
  350.     stream.Read(&fPenSize, sizeof(fPenSize));
  351.     fPattern.Unflatten(stream);
  352.     
  353.     FW_ByteCount count;
  354.     stream >> count;
  355.  
  356.     if (count != 0)
  357.     {
  358.         char temp[256];
  359.         stream.Read(temp, count);
  360.         fFontName.ReplaceAll(temp);
  361.     }
  362.     else
  363.     {
  364.         fFontName = "";
  365.     }
  366.     
  367.     stream.Read((char*)&fFontStyle, sizeof(fFontStyle));
  368.     stream >> fFontSize;
  369.     
  370. #ifdef FW_BUILD_MAC    
  371.     fMacCacheValid = FALSE;
  372. #endif
  373. }
  374.  
  375. #ifdef FW_BUILD_MAC
  376. //----------------------------------------------------------------------------------------
  377. //    • FW_CStyleRep::CheckMacCache
  378. //----------------------------------------------------------------------------------------
  379.  
  380. void FW_CStyleRep::CheckMacCache()
  381. {
  382.     if (!fMacCacheValid)
  383.     {
  384.         // ----- fMacTextFace -----
  385.         switch (fFontStyle)
  386.         {
  387.             case FW_kPlain:
  388.                 fMacTextFace = normal;
  389.                 break;
  390.             case FW_kBold:
  391.                 fMacTextFace = bold;
  392.                 break;
  393.             case FW_kItalic:
  394.                 fMacTextFace = italic;
  395.                 break;
  396.             case FW_kUnderline:
  397.                 fMacTextFace = underline;
  398.                 break;
  399.             case FW_kStrikeThrough:
  400.                 fMacTextFace = normal;
  401.                 break;
  402.             case FW_kOutline:
  403.                 fMacTextFace = outline;
  404.                 break;
  405.             case FW_kShadow:
  406.                 fMacTextFace = shadow;
  407.                 break;
  408.             case FW_kExtended:
  409.                 fMacTextFace = extend;
  410.                 break;
  411.             case FW_kCondensed:
  412.                 fMacTextFace = condense;
  413.                 break;
  414.         };
  415.         
  416.         // ----- fMacTextFont -----
  417.         short fontNumber;
  418.         Str255 pascalFontName;
  419.         
  420.         fFontName.ExportPascal(pascalFontName);
  421.         ::GetFNum(pascalFontName, &fontNumber);
  422.         
  423.         if (fontNumber == systemFont)
  424.         {
  425.             // fontNum is set to sysFont if fontName is not found.
  426.             // We must check for the special case where the system
  427.             // font is indeed being retrieved, by comparing the
  428.             // name of the font with that of the system font.    
  429.             Str255 sysFontName;
  430.             ::GetFontName(systemFont, sysFontName);
  431.             if (!::EqualString(pascalFontName, sysFontName, FALSE, FALSE))
  432.             {
  433. //                // font was not found
  434. //                BR_THROW(BR_XFontNameNotFound(fFaceName));
  435.             }
  436.         }
  437.         fMacTextFont = fontNumber;
  438.  
  439.         fMacCacheValid = TRUE;
  440.     }
  441. }
  442.  
  443. #endif